﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using LKVService;
using LKVMLMLib;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI;
using LKVMLMADMIN.Models;
namespace LKVMLMADMIN.Controllers
{
    public class ProfitController : Controller
    {
        public LKVService.User userService = new LKVService.User();
        public int IdChuKy = 0;
        public int idchuky = 0;
        [AllowAnonymous]
        public ActionResult Commission(int page = 1, int user = 0, string Hoahong = "", string ChuKy = "", int month = 0, int selectedyear = 0, int soluong = 0)
        {
            var Profitservice = new LKVService.ProfitService();
            ViewBag.Title = "Danh sách hoa hồng";
            if (Session["User"] != null)
            {
                string TenHoaHong = "";
                if (Hoahong != "1")
                    TenHoaHong = Hoahong;
                var Listuser = userService.getListUser();
                var ListUsername = Listuser.Select(x => new LKVMLMADMIN.Models.loadCommissionModels { userid = x.UserId, UserName = x.UserName }).ToList();
                ListUsername.Insert(0, new LKVMLMADMIN.Models.loadCommissionModels { userid = 0, UserName = "Chọn mã User" });

                ViewBag.Username = ListUsername;
                if (month == 0)
                    month = DateTime.Now.Month;
                ViewBag.Month = month;
                if (selectedyear == 0)
                    selectedyear = DateTime.Now.Year;
                List<string> yd = new List<string>();
                for (int y = 2013; y <= DateTime.Now.Year; y++)
                    yd.Add(y.ToString());
                ViewBag.Year = yd;
                ViewBag.Selectedyear = Convert.ToInt32(selectedyear);
                var ListCk = Profitservice.getChukyConfig().ToArray();
                string a = "";
                foreach (var ck in (ChuKyConfig[])ListCk)
                {
                    if (DateTime.Now.Day >= ck.From && DateTime.Now.Day <= ck.To)
                    {
                        a = Profitservice.findChuKyByID(ck.ChuKyID).TenChuKy;
                        break;
                    }

                }
                ViewBag.Chuky = a;
                ViewBag.Listck = ListCk.Select(x => x.TenChuKy).ToList();
                var _lb1 = Profitservice.getListCommissionConfig(true, false).Select(s => new LKVMLMADMIN.Models.LoadLoaiHoaHongModels { CommissionName = s.CommissionName, CommissionDesc = s.CommissionDesc }).ToList();
                _lb1.Insert(0, new LKVMLMADMIN.Models.LoadLoaiHoaHongModels { CommissionName = "1", CommissionDesc = "Hoa Hồng" });
                ViewBag.Listhoahong = _lb1;

                if (ChuKy != "")
                {
                    IdChuKy = Profitservice.getChukyConfig().Where(w => w.TenChuKy.Contains(ChuKy)).FirstOrDefault().ChuKyID;
                    ViewBag.ChuKySelect = Profitservice.getChukyConfig().Where(w => w.TenChuKy.Contains(ChuKy)).FirstOrDefault().TenChuKy;
                }
                var extenssion = new LKVService.User();
                string tenuser = "";
                var userchoose = extenssion.findUserById(user);
                if (userchoose != null)
                    tenuser = userchoose.UserName;
                var ListCommission = Profitservice.getlistCommissionPaging("", tenuser, TenHoaHong, month, selectedyear, IdChuKy, (page - 1) * 100, 100);

                var ArrayCommission = ListCommission.Item3;
                ViewBag.ListCommission = ArrayCommission;
                double? Tong = 0;
                Tong = ListCommission.Item3.Sum(s => s.Amount);
                ViewBag.Tong = Tong;

                int[] myArray = Enumerable.Repeat(-1, ListCommission.Item1).ToArray();

                return View(myArray.ToPagedList(page, 100));
            }
            else
                return RedirectToAction("Login", "Account");
        }
      

    }
}
